package exercise;

class Solution3 {
    public int bitSum (int n) {
        int sum = 0;
        while (n != 0) {
            sum += Math.pow(n % 10, 2);
            n /= 10;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        // 快慢指针
        int slow = n, fast = bitSum(n);

        while (slow != fast) {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }

        return slow == 1;
    }
}